package com.woniuxy;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @Author WY
 * @Description 生产者，往mq中发送一个消息
 * @PackageName phase-iv-cyx
 * @Package com.woniuxy
 * @Date 2022/12/29 11:26
 * @Version 1.0
 */
public class MQSendMessage {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //下列4个配置都是默认的配置
        connectionFactory.setHost("localhost");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setPort(5672);//5672是MQ的服务端的端口，15672是可视化页面的端口
        //通过链接工厂创建链接，由链接创建channel信道。
        Channel channel = connectionFactory.newConnection().createChannel();
        //绑定信道和队列
        channel.queueDeclare("my_queue", false, false, false, null);
        String msg = "hello RabbitMQ";
        //发送消息
        channel.basicPublish("","my_queue",null,msg.getBytes(StandardCharsets.UTF_8));
        System.out.println("Send over");
        //关闭资源
        channel.close();
        connectionFactory.clone();
    }
}
